package com.turnengine.client.local.action.bean;

import com.turnengine.client.local.action.enums.ActionTargetType;
import com.turnengine.client.local.unit.enums.UnitRelation;
import com.turnengine.client.local.unit.enums.UnitType;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Action Target.
 */
public class ActionTarget implements IActionTarget, Comparable<IActionTarget> {

	/** The action id. */
	private int actionId = -1;
	/** The target type. */
	private ActionTargetType targetType = ActionTargetType.SOURCE;
	/** The type. */
	private UnitType type = UnitType.PLAYER;
	/** The relation. */
	private UnitRelation relation = UnitRelation.ANY;

	/**
	 * Creates a new Action Target.
	 */
	public ActionTarget() {
	}

	/**
	 * Creates a new Action Target.
	 * @param actionId the action id
	 * @param targetType the target type
	 * @param type the type
	 * @param relation the relation
	 */
	public ActionTarget(int actionId, ActionTargetType targetType, UnitType type, UnitRelation relation) {
		setActionId(actionId);
		setTargetType(targetType);
		setType(type);
		setRelation(relation);
	}

	/**
	 * Creates a new Action Target.
	 * @param actionTarget the action target
	 */
	public ActionTarget(ActionTarget actionTarget) {
		setActionId(actionTarget.getActionId());
		setTargetType(actionTarget.getTargetType());
		setType(actionTarget.getType());
		setRelation(actionTarget.getRelation());
	}

	/**
	 * Creates a new Action Target.
	 * @param iActionTarget the i action target
	 */
	public ActionTarget(IActionTarget iActionTarget) {
		setActionId(iActionTarget.getActionId());
		setTargetType(iActionTarget.getTargetType());
		setType(iActionTarget.getType());
		setRelation(iActionTarget.getRelation());
	}

	/**
	 * Returns the action id.
	 * @return the action id.
	 */
	public int getActionId() {
		return actionId;
	}

	/**
	 * Returns the target type.
	 * @return the target type.
	 */
	public ActionTargetType getTargetType() {
		return targetType;
	}

	/**
	 * Returns the type.
	 * @return the type.
	 */
	public UnitType getType() {
		return type;
	}

	/**
	 * Returns the relation.
	 * @return the relation.
	 */
	public UnitRelation getRelation() {
		return relation;
	}

	/**
	 * Sets the action id.
	 * @param actionId the action id to set.
	 */
	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	/**
	 * Sets the target type.
	 * @param targetType the target type to set.
	 */
	public void setTargetType(ActionTargetType targetType) {
		this.targetType = targetType;
	}

	/**
	 * Sets the type.
	 * @param type the type to set.
	 */
	public void setType(UnitType type) {
		this.type = type;
	}

	/**
	 * Sets the relation.
	 * @param relation the relation to set.
	 */
	public void setRelation(UnitRelation relation) {
		this.relation = relation;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(373, 37);
		hash.append(actionId);
		hash.append(targetType);
		hash.append(type);
		hash.append(relation);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof ActionTarget) {
			ActionTarget compare = (ActionTarget) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.actionId, compare.actionId);
			equals.append(this.targetType, compare.targetType);
			equals.append(this.type, compare.type);
			equals.append(this.relation, compare.relation);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IActionTarget compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.actionId, compare.getActionId());
		builder.append(this.targetType, compare.getTargetType());
		builder.append(this.type, compare.getType());
		builder.append(this.relation, compare.getRelation());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("actionId", actionId);
		if (targetType != null) {
			string.append("targetType", targetType);
		}
		if (type != null) {
			string.append("type", type);
		}
		if (relation != null) {
			string.append("relation", relation);
		}
		return string.toString();
	}
}
